package com.cow.sram.module.distribution;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cow.framework.utils.reflection.MapToBeanUtil;
import com.cow.framework.utils.web.IPUtil;
import com.cow.framework.utils.web.SessionUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.data.dao.OperatelogDAO;
import com.cow.sram.data.entity.Operatelog;
import com.cow.sram.data.entity.Userrole;
import com.cow.sram.module.authority.RoleInfoService;
import com.github.pagehelper.Page;
/**
 * 用户角色
 * @author Administrator
 *
 */
@RestController
@EnableAutoConfiguration
@Transactional
@RequestMapping(value="/distribution")
public class UserroleController extends BaseComponent{
	@Autowired
	UserroleService userroleService;
	@Autowired
	RoleInfoService roleInfoService;
	@Autowired
	OperatelogDAO operatelogDAO;
	
	/**查询用户可分配的角色列表*/
	@RequestMapping(value="/findUserrole")
	public Object findUserrole(@RequestParam Integer userid,@RequestParam(required=false) Integer roletype){
		Map parmMap = new HashMap();
		parmMap.put("userid", userid);
		parmMap.put("roletype", roletype);
		Page<Map> roleinfoList = userroleService.findUserRole(parmMap);
		for(Map info:roleinfoList){
			//判断该角色是否已分配此资源权限
			if(info.get("privilegetype")!=null){
				info.put("grantFlag", 1);
			}else{
				info.put("privilegetype", null);
				info.put("grantFlag", 0);
			}
			
		}
		return roleinfoList;
	}
	
	/**根据ID查看用户角色*/
	@RequestMapping(value="/getUserroleById")
	public Object getUserroleById(@RequestParam Integer id){
		return userroleService.getUserroleById(id);
	}
	
	/**新增记录*/
	@RequestMapping(value="/saveUserrole")
	public Object saveUserrole(@RequestBody Userrole rsc){
		rsc.setGrantor(super.getCurUserId());
		userroleService.newUserrole(rsc);
		return "success";
	}
	
	/**修改*/
	@RequestMapping(value="/editUserrole")
	public Object editUserrole(@RequestBody Map parmMap){
		//根据Id查询用户角色
		Userrole rsc=userroleService.getUserroleById(Integer.valueOf(parmMap.get("mapid").toString()));
		MapToBeanUtil.copyMapToBean(parmMap, rsc);  //拷贝到实体
		userroleService.newUserrole(rsc);
		return "success";
		
	}
	
	/**保存*/
	@RequestMapping(value="/setUserRole")
	public Object setUserRole(@RequestBody SetUserRoleModel parm){
		//删除用户原有角色
		userroleService.delUserRole(parm.getUserid());
		//重新授权角色
		StringBuffer roleBf = new StringBuffer();
		if(parm.getUserRoleList() != null){
			for(Userrole userrole:parm.getUserRoleList()){
				userrole.setUserid(parm.getUserid());
				userrole.setGrantor(super.getCurUserId());
				userroleService.newUserrole(userrole);
				roleBf.append(userrole.getRoleid());
			}
		}
		 // 接收到请求，记录请求内容
        HttpServletRequest request = SessionUtil.getRequest();        
        String ip = IPUtil.getIpAddress(request);
        String uri = request.getRequestURI();
		//记录访问日志
    	Operatelog log = new Operatelog();
    	log.setAccountname(super.getCurUser().getAccountname());
    	log.setResourcepath(uri + "******userid:"+parm.getUserid() +" roleid:"+roleBf.toString());
    	log.setOpername("角色授权");
    	log.setOperip(ip);
    	log.setOpertime(new Date());
    	operatelogDAO.save(log); 
    	
		return "success";
		
	}
	
//	/**保存*/
//	@RequestMapping(value="/setUserRole")
//	public Object setUserRole(@RequestBody Map parmMap){
//		//重新授权角色
//		String roleIds = (String)parmMap.get("roleIds");
//		Integer userId=Integer.valueOf(parmMap.get("userId").toString());
//		if(roleIds==null || roleIds==""){
//		//删除用户原有角色
//		userroleService.delUserId(userId);
//		}else{
//			//删除用户原有角色
//		userroleService.delUserId(userId);
//		String[] roleArray = roleIds.split(",");
//		for(String roleid:roleArray){
//			Userrole ur=new Userrole();
//			ur.setUserid(userId);
//			ur.setRoleid(Integer.valueOf(roleid));
//			ur.setGrantor(super.getCurUserId());
//			userroleService.newUserrole(ur);
//		}
//		}
//		return "success";
//		
//	}
	
	/**删除用户角色*/
	@RequestMapping(value="/delUserrole")
	public Object delUserrole(@RequestParam Integer id){
		userroleService.delUserrole(id);
		return "success";
	}
}
